package ws

import (
	"net"
	"wzgames/pkg/actor/actor"
)

type SessionInit struct{}
type SessionAccepted struct{}
type SessionConnected struct{}
type SessionConnectError struct{}
type SessionClosed struct {
	C int
}

type WsConfig struct {
	ServID int64  `yaml:"serv_id"`
	Addr   string `yaml:"addr"`
}

type ISession interface {
	Acceptor() IWebSocketAcceptor
	Raw() interface{} // raw conn
	Send(msg interface{})
	Close()

	ID() int64
	SetID(id int64)

	SetLink(*actor.PID, int64)
	LinkPid() *actor.PID
	LinkID() int64

	Ok() bool
	Token() string
	RemoteIP() string
}

type IWebSocketAcceptor interface {
	Start() IWebSocketAcceptor
	Stop()
	Name() string
	Addr() string
	IsReady() bool
	SetCerts(certfile, keyfile string)
	SetUpgrader(upgrader interface{})
	Port() int
	Server() *WebSocket
}

type MsgEvent struct {
	Sess ISession
	Msg  interface{}
}

func (evt *MsgEvent) Session() ISession {
	return evt.Sess
}

func (evt *MsgEvent) Message() interface{} {
	return evt.Msg
}

type RemoteAddr interface {
	RemoteAddr() net.Addr
}

func GetRemoteAddrss(ses ISession) (string, bool) {
	if ses == nil {
		return "", false
	}

	if c, ok := ses.Raw().(RemoteAddr); ok {
		return c.RemoteAddr().String(), true
	}

	return "", false
}
